<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          @submit.native.prevent
          v-show="showSearch"
          label-width="68px"
        >
        </el-form>
        <div class="flex-1"></div>
        <div>
          <div class="flex-1"></div>
          <div style="display: flex">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" @click="handleReset"
              >重置</el-button
            >
          </div>
        </div>
      </div>
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-permission="['TOURISTUSER_ADD_BUTTON']"
            >新增</el-button
          >
          <el-button
            type="success"
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-permission="['TOURISTUSER_UPDATE_BUTTON']"
            >修改</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-permission="['TOURISTUSER_DEL_BUTTON']"
            >删除</el-button
          >
          <el-button
            type="warning"
            icon="el-icon-download"
            @click="handleExport"
            v-permission="['TOURISTUSER_EXPORT_BUTTON']"
            >导出</el-button
          >
          <div class="flex-1"></div>
          <dt-dialog-column
            v-model="isShowTable"
            :columns="showColumns"
            :table-ref="$refs.table"
            @queryTable="getList"
          />
        </div>
        <el-table
          v-if="isShowTable"
          v-loading="loading"
          ref="table"
          border
          highlight-current-row
          :header-cell-style="{ backgroundColor: '#f2f2f2' }"
          :data="TouristUserList"
          @selection-change="handleSelectionChange"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="selection"
            width="55"
            align="center"
          />
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="
              (index) =>
                (queryParams.pageNo - 1) * queryParams.pageSize + index + 1
            "
          />
          <el-table-column
            label="用户id"
            v-if="showColumns[0].show"
            show-overflow-tooltip
            key="userId"
            align="center"
            prop="userId"
          />
          <el-table-column
            label="移动端mac地址"
            v-if="showColumns[1].show"
            show-overflow-tooltip
            key="mobileMac"
            align="center"
            prop="mobileMac"
          />
          <el-table-column
            label="PC端mac地址"
            v-if="showColumns[2].show"
            show-overflow-tooltip
            key="pcMac"
            align="center"
            prop="pcMac"
          />
          <el-table-column
            label="是否删除：Y-已删除，N-未删除"
            v-if="showColumns[3].show"
            show-overflow-tooltip
            key="delFlag"
            align="center"
            prop="delFlag"
          />
          <el-table-column
            label="创建时间"
            v-if="showColumns[4].show"
            show-overflow-tooltip
            key="createTime"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="创建人"
            v-if="showColumns[5].show"
            show-overflow-tooltip
            key="createUser"
            align="center"
            prop="createUser"
          />
          <el-table-column
            label="修改时间"
            v-if="showColumns[6].show"
            show-overflow-tooltip
            key="updateTime"
            align="center"
            prop="updateTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="修改人"
            v-if="showColumns[7].show"
            show-overflow-tooltip
            key="updateUser"
            align="center"
            prop="updateUser"
          />
          <el-table-column
            label="操作"
            :key="Math.random()"
            fixed="right"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-permission="['TOURISTUSER_UPDATE_BUTTON']"
                >修改</el-button
              >
              <el-button size="small" @click="handleDetail(scope.row)"
                >查看</el-button
              >
              <el-button
                size="small"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-permission="['TOURISTUSER_DEL_BUTTON']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <dt-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 添加或修改游客用户关联对话框 -->
    <dt-dialog
      :title="title"
      :visible.sync="open"
      :is-loading="loading"
      @comfirmBtn="submitForm"
      @closeDialog="cancel"
    >
      <div slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="用户id" prop="userId">
            <el-input
              v-model.trim="form.userId"
              placeholder="请输入用户id"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="移动端mac地址" prop="mobileMac">
            <el-input
              v-model.trim="form.mobileMac"
              placeholder="请输入移动端mac地址"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="PC端mac地址" prop="pcMac">
            <el-input
              v-model.trim="form.pcMac"
              placeholder="请输入PC端mac地址"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="是否删除：Y-已删除，N-未删除" prop="delFlag">
            <el-input
              v-model.trim="form.delFlag"
              placeholder="请输入是否删除：Y-已删除，N-未删除"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              clearable
              v-model="form.createTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择创建时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="创建人" prop="createUser">
            <el-input
              v-model.trim="form.createUser"
              placeholder="请输入创建人"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="修改时间" prop="updateTime">
            <el-date-picker
              clearable
              v-model="form.updateTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择修改时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="修改人" prop="updateUser">
            <el-input
              v-model.trim="form.updateUser"
              placeholder="请输入修改人"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
        </el-form>
      </div>
    </dt-dialog>
    <!-- 详情弹框 -->
    <dt-detail ref="dtDetailRef" :dt-detail-option="detailOption" />
  </div>
</template>

<script>
import {
  listTouristUser,
  getTouristUser,
  delTouristUser,
  addTouristUser,
  updateTouristUser,
} from "@/api/touristUser/TouristUser";
export default {
  name: "TouristUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      //显隐表格
      isShowTable: true,
      // 选中数组
      ids: [],
      //列显隐数组
      showColumns: [
        { prop: "userId", label: "用户id", show: true },
        { prop: "mobileMac", label: "移动端mac地址", show: true },
        { prop: "pcMac", label: "PC端mac地址", show: true },
        { prop: "delFlag", label: "是否删除：Y-已删除，N-未删除", show: true },
        { prop: "createTime", label: "创建时间", show: true },
        { prop: "createUser", label: "创建人", show: true },
        { prop: "updateTime", label: "修改时间", show: true },
        { prop: "updateUser", label: "修改人", show: true },
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 游客用户关联表格数据
      TouristUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  computed: {
    //详情参数
    detailOption() {
      return {
        rows: [
          {
            label: "用户id",
            prop: "userId",
          },
          {
            label: "移动端mac地址",
            prop: "mobileMac",
          },
          {
            label: "PC端mac地址",
            prop: "pcMac",
          },
          {
            label: "是否删除：Y-已删除，N-未删除",
            prop: "delFlag",
          },
          {
            label: "创建时间",
            prop: "createTime",
          },
          {
            label: "创建人",
            prop: "createUser",
          },
          {
            label: "修改时间",
            prop: "updateTime",
          },
          {
            label: "修改人",
            prop: "updateUser",
          },
        ],
        data: {},
      };
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询游客用户关联列表 */
    getList() {
      this.loading = true;
      listTouristUser(this.queryParams).then(({ data: response }) => {
        this.TouristUserList = response.rows;
        this.total = response.totalRows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        mobileMac: null,
        pcMac: null,
        delFlag: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加游客用户关联";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTouristUser(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改游客用户关联";
      });
    },
    handleDetail(row) {
      this.detailOption.data = row;
      this.$refs.dtDetailRef.dialogVisible = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTouristUser(this.form).then((response) => {
              this.$dtModal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTouristUser(this.form).then((response) => {
              this.$dtModal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$dtModal
        .confirm('是否确认删除游客用户关联编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTouristUser(ids);
        })
        .then(() => {
          this.getList();
          this.$dtModal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "touristUser/TouristUser/export",
        {
          ...this.queryParams,
        },
        `touristUser_TouristUser.xlsx`
      );
    },
  },
};
</script>
